<?php

/**
* ShowCodeFormModule.class.php
*
* Concrete page module responsable for code show
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/

require_once("framework/config.php");
require_once("framework/PageModule.class.php");
require_once("HTML/QuickForm.php");
require_once("HTML/QuickForm/Renderer/Default.php");
require_once("framework/Wurfl.class.php");
require_once("framework/Yt.class.php");

// {{{ SlideShowListModule

class ShowCodeFormModule extends PageModule 
{
    // {{{ Class operations 

    // {{{ SlideShowListModule
    /**
    * C'tor
    */
    
    function ShowCodeFormModule()
    {
        trigger_error("ENTER ShowCodeFormModule::ShowCodeFormModule()", E_USER_NOTICE);
    }
    // }}}
    // {{{ getContent

    /**
    * Function returns page content
    *
    */

    function getContent()
    {

        trigger_error("ENTER ShowCodeFormModule::getContent()", E_USER_NOTICE);
        $options = &PEAR::getStaticProperty('CONFIG_INI','TEMPLATES'); 
 
        $tpl = new HTML_Template_IT($options["templates_dir"]);
        $tpl->loadTemplatefile("show_code.tpl.xhtml",true,true);

        $statusTpl = new HTML_Template_IT($options["templates_dir"]);
        $statusTpl->loadTemplatefile("status.tpl.xhtml",FALSE,FALSE);
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $dbh = new PDO($options['dsn_string']);
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sth = $dbh->prepare("SELECT COUNT(rowid) as count, 
            *,
            (SELECT name FROM guide_name WHERE guide_name.locale = :locale AND guide_id = :id) AS name,
            (SELECT desc FROM guide_desc WHERE guide_desc.locale = :locale AND guide_id = :id) AS desc  
            FROM guide WHERE rowid = :id AND publish = 1");
        $sth->bindParam(':locale',  LocalizationSingleton::getLocale(), PDO::PARAM_STR);
        $sth->bindParam(':id',$_GET['id']);
        $sth->execute();
        $guideRow = $sth->fetch(PDO::FETCH_ASSOC);

        if( $guideRow['count'] != 0 )
        {
            $statusTpl->setVariable("STATUS",_("Opis trofeum i jego lokalizacja."));
            $uploadOpt = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
            $globalOpt = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');

            $tpl->setVariable("NAME",$guideRow['name']);

            if ( $guideRow['pic_file'] ) 
            {
                $tpl->setCurrentBlock('image_row');
                $tpl->setVariable("URL",rtrim($globalOpt['service_url'],'/')."/send_image.php?id=".$guideRow['pic_file']);
                $tpl->setVariable("ALT",$guideRow['pic_file']);
                $tpl->parseCurrentBlock();
            }

            if ( $guideRow['desc'] ) 
            {
                $tpl->setCurrentBlock('desc_row');
                $tpl->setVariable("DESC",$guideRow['desc']);
                $tpl->parseCurrentBlock();
            }

            if ( Wurfl::deviceFactory()->getCapability("streaming_video") )
            {
                if ( $guideRow['video_file'] ) 
                {

                    $yt = Yt::getClient();
                    try
                    {
                        $videoEntry = $yt->getVideoEntry($guideRow['video_file']);
                        if (Wurfl::deviceFactory()->getCapability("model_name") == 'iPhone' || 
                            Wurfl::deviceFactory()->getCapability("is_wireless_device") != 'true' )
                        {
 
                            foreach ($videoEntry->mediaGroup->content as $content) {
                                if ( $content->format === '5') {
                                    $tpl->setCurrentBlock('video_embed_row');
                                    $tpl->setVariable("URL",$content->url);
                                    $tpl->setVariable("CONTENTTYPE",$content->type);
                                    $height = Wurfl::deviceFactory()->getCapability("max_image_height");
                                    $tpl->setVariable("HEIGHT", $height > 640 ? 640 : $height );
                                    $width = Wurfl::deviceFactory()->getCapability("max_image_width");
                                    $tpl->setVariable("WIDTH", $width > 640 ? 640 : $width );
                                    $tpl->parseCurrentBlock();

                                }
                            }
                        }                                               
                        elseif (Wurfl::deviceFactory()->getCapability("streaming_vcodec_mpeg4_sp") >=0)
                        {
                            foreach ($videoEntry->mediaGroup->content as $content) {
                                if ( $content->format === '6') {
                                    $tpl->setCurrentBlock('video_link_row');
                                    $tpl->setVariable("URL",$content->url);
                                    $tpl->setVariable("TITLE",_('Pobierz video'));
                                    $tpl->parseCurrentBlock();
                                }
                            }
                        }
                        elseif( Wurfl::deviceFactory()->getCapability("streaming_vcodec_h263_0") >= 0 || 
                                Wurfl::deviceFactory()->getCapability("streaming_vcodec_h263_3") >= 0 )
                        {
                            foreach ($videoEntry->mediaGroup->content as $content) {
                                if ( $content->format === '1') {
                                    $tpl->setCurrentBlock('video_link_row');
                                    $tpl->setVariable("URL",$content->url);
                                    $tpl->setVariable("TITLE",_('Pobierz video'));
                                    $tpl->parseCurrentBlock();
                                }
                            }

                        }
                        else
                        {
                            $tpl->setCurrentBlock('video_status');
                            $tpl->setVariable("TEXT",_('Wideo niedostępne na twoim telefonie.'));
                            $tpl->parseCurrentBlock();
                        }


                    }
                    catch ( Zend_Exception $e )
                    {
                        $tpl->setCurrentBlock('video_status');
                        $tpl->setVariable("TEXT",_('Wideo w trakcie przetwarzania'));
                        $tpl->parseCurrentBlock();
                    }
            


                }
            }
            if ( $guideRow['geocode'] ) 
            {
                $tpl->setCurrentBlock('location_row');
                $tpl->setVariable("LOCATION",$guideRow['geocode']);
                $tpl->parseCurrentBlock();
                $tpl->setCurrentBlock('map_row');
                $tpl->setVariable("ALT",$guideRow['geocode']);
                $tpl->setVariable("MARKERSIZE","tiny");
                $tpl->setVariable("GEOCODE",urlencode($guideRow['geocode']));

                $tpl->setVariable("SENSOR","true");

                $height = Wurfl::deviceFactory()->getCapability("max_image_height");
                $tpl->setVariable("HEIGHT", $height > 640 ? 640 : $height );

                $width = Wurfl::deviceFactory()->getCapability("max_image_width");
                $tpl->setVariable("WIDTH", $width > 640 ? 640 : $width );

                $tpl->parseCurrentBlock();
            }
        }
        else
        {
            $statusTpl->setVariable("STATUS",_('Kod nie jest poprawny.'));
        } 


        return $statusTpl->get().$tpl->get().$statusTpl->get();
    }
    
    // }}}

    // }}}
}

// }}} 
?>
